package com.yex.controller.modify.order;

import com.baomidou.mybatisplus.plugins.Page;
import com.yex.common.exception.RRException;
import com.yex.common.utils.R;
import com.yex.entity.MerchantOrderEntity;
import com.yex.entity.UserEntity;
import com.yex.entity.vo.MerchantOrderVo;
import com.yex.service.modify.order.MerchantOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/appApi/merchantOrder")
public class MerchantOrderController {

    @Autowired
    private MerchantOrderService merchantOrderService;

    @GetMapping("pageList")
    public R pageList(MerchantOrderEntity merchantOrderEntity, @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber, @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        if (merchantOrderEntity.getOrderType() != 1 && merchantOrderEntity.getOrderType() != 2)
            throw new RRException("请选择订单类型");
        Page<MerchantOrderVo> datas = merchantOrderService.pageList(merchantOrderEntity, pageNumber, rows, false);
        return R.okData(datas);
    }

    @GetMapping("orderList")
    public R orderList(MerchantOrderEntity merchantOrderEntity, @RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber, @RequestParam(value = "rows", defaultValue = "10") Integer rows) {
        if (merchantOrderEntity.getOrderType() != 1 && merchantOrderEntity.getOrderType() != 2)
            throw new RRException("请选择订单类型");
        Page<MerchantOrderVo> datas = merchantOrderService.pageList(merchantOrderEntity, pageNumber, rows, true);
        return R.okData(datas);
    }

    @PostMapping("createOrder")
    public R createOrder(MerchantOrderEntity merchantOrderEntity) {
        if (merchantOrderEntity.getPrice() == null) throw new RRException("请输入单价");
        if (merchantOrderEntity.getTotal() == null) throw new RRException("请输入数量");
//        if (merchantOrderEntity.getBankId() == null) throw new RRException("请选择收款银行卡");
        if (merchantOrderEntity.getMinSellerNumber() == null) throw new RRException("请输入最小购买数量");
        if (merchantOrderEntity.getMaxSellerNumber() == null) throw new RRException("请输入最大购买数量");
        if (merchantOrderEntity.getOrderType() != 1 && merchantOrderEntity.getOrderType() != 2)
            throw new RRException("请选择订单类型");
        if (merchantOrderEntity.getMinSellerNumber().compareTo(merchantOrderEntity.getMaxSellerNumber()) > 0)
            throw new RRException("最小购买数量必须小于最大购买数量");
        MerchantOrderEntity merchantOrderEntity1 = merchantOrderService.createOrder(merchantOrderEntity);
        return R.okData(merchantOrderEntity1);
    }

    @PostMapping("cancelOrder")
    public R cancelOrder(Integer id) {
        if (id == null) throw new RRException("缺少参数: id");
        merchantOrderService.cancelOrder(id);
        return R.ok("撤销挂单成功");
    }

}
